/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 21:54:56
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpWorkSafetyApplyFireListBean;
import com.joysuch.wwyt.bp.bean.BpWorkSafetyCertDetailBean;
import com.joysuch.wwyt.bp.bean.safetyworkcert.BpWorkSafetyCertSearchBean;
import com.joysuch.wwyt.bp.bean.safetyworkcert.BpWorkSafetyCertYiBanEditBean;
import com.joysuch.wwyt.bp.entity.BpWorkSafetyCert;
import com.joysuch.wwyt.bp.enums.SafetyWorkTypes;
import com.joysuch.wwyt.bp.service.BpWorkSafetyCertService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/bp/work-safety-apply-yiban")
@Api(tags = "/bp/work-safety-apply-yiban 一般作业证")
public class BpWorkSafetyCertYiBanController {

    private static final Logger logger = LoggerFactory.getLogger(BpWorkSafetyCertYiBanController.class);

    @Autowired
    private BpWorkSafetyCertService bpWorkSafetyCertService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation("添加")
    public Object add(@Valid @RequestBody BpWorkSafetyCertYiBanEditBean entity) {
        String module = "一般作业证";
//        try {
        if (!"0".equals(entity.getJobType())) {
            // 0是其他，非其他类型的字段，不接受该值
            entity.setOtherTypeContent("");
        }
        BpWorkSafetyCert add = bpWorkSafetyCertService.add(entity, SafetyWorkTypes.YI_BAN, module);
//        } catch (Exception e) {
//            logger.error("添加一般作业证#" + entity.getId() + "出错", e);
//        }
        return ResultBean.success(add);
    }

    @RequestMapping("update")
    public Object update(@Valid @RequestBody BpWorkSafetyCertYiBanEditBean entity) throws Exception {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        String module = "一般作业证";
//        try {
            if (!"0".equals(entity.getJobType())) {
                // 0是其他，非其他类型的字段，不接受该值
                entity.setOtherTypeContent("");
            }
        BpWorkSafetyCert update = bpWorkSafetyCertService.update(entity, SafetyWorkTypes.YI_BAN, module);
//        } catch (Exception e) {
//            logger.error("更新一般作业证#" + entity.getId() + "出错", e);
//        }
        return ResultBean.success(update);
    }

    @RequestMapping("page")
    public Object page(Pageable page, BpWorkSafetyCertSearchBean condition) {
        Page<BpWorkSafetyApplyFireListBean> data = bpWorkSafetyCertService.findByPage(page, condition,
                SafetyWorkTypes.YI_BAN);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("get")
    public Object getById(Long id) {
        Assert.notNull(id, "id不能为空");
        BpWorkSafetyCertDetailBean bean = bpWorkSafetyCertService.getById(id);
        return ResultBean.success(bean);
    }

    @RequestMapping("delete")
    public Object delete(Long id) throws BusinessRestrictionsException {
        // bpWorkSafetyApplyFireService.delete(id);
        bpWorkSafetyCertService.delete(id);
        return ResultBean.success("");
    }

}
